home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dskut / patch12.zip / PCH.C < prev    next >
C/C++ Source or Header  |  1990-05-30  |  30KB  |  1,124 lines

  1. /* $Header: pch.c,v 2.0.1.7 88/06/03 15:13:28 lwall Locked $
  2.  *
  3.  * $Log:    pch.c,v $
  4.  * Revision 2.0.1.7  88/06/03  15:13:28  lwall
  5.  * patch10: Can now find patches in shar scripts.
  6.  * patch10: Hunks that swapped and then swapped back could core dump.
  7.  * 
  8.  * Revision 2.0.1.6  87/06/04  16:18:13  lwall
  9.  * pch_swap didn't swap p_bfake and p_efake.
  10.  * 
  11.  * Revision 2.0.1.5  87/01/30  22:47:42  lwall
  12.  * Improved responses to mangled patches.
  13.  * 
  14.  * Revision 2.0.1.4  87/01/05  16:59:53  lwall
  15.  * New-style context diffs caused double call to free().
  16.  * 
  17.  * Revision 2.0.1.3  86/11/14  10:08:33  lwall
  18.  * Fixed problem where a long pattern wouldn't grow the hunk.
  19.  * Also restored p_input_line when backtracking so error messages are right.
  20.  * 
  21.  * Revision 2.0.1.2  86/11/03  17:49:52  lwall
  22.  * New-style delete triggers spurious assertion error.
  23.  * 
  24.  * Revision 2.0.1.1  86/10/29  15:52:08  lwall
  25.  * Could falsely report new-style context diff.
  26.  * 
  27.  * Revision 2.0  86/09/17  15:39:37  lwall
  28.  * Baseline for netwide release.
  29.  * 
  30.  */
  31.  
  32. #include "EXTERN.h"
  33. #include "common.h"
  34. #include "util.h"
  35. #include "INTERN.h"
  36. #include "pch.h"
  37.  
  38. /* Patch (diff listing) abstract type. */
  39.  
  40. static long p_filesize;            /* size of the patch file */
  41. static LINENUM p_first;            /* 1st line number */
  42. static LINENUM p_newfirst;        /* 1st line number of replacement */
  43. static LINENUM p_ptrn_lines;        /* # lines in pattern */
  44. static LINENUM p_repl_lines;        /* # lines in replacement text */
  45. static LINENUM p_end = -1;        /* last line in hunk */
  46. static LINENUM p_max;            /* max allowed value of p_end */
  47. static LINENUM p_context = 3;        /* # of context lines */
  48. static LINENUM p_input_line = 0;    /* current line # from patch file */
  49. static char **p_line = Null(char**);    /* the text of the hunk */
  50. static short *p_len = Null(short*);    /* length of each line */
  51. static char *p_char = Nullch;        /* +, -, and ! */
  52. static int hunkmax = INITHUNKMAX;    /* size of above arrays to begin with */
  53. static int p_indent;            /* indent to patch */
  54. static LINENUM p_base;            /* where to intuit this time */
  55. static LINENUM p_bline;            /* line # of p_base */
  56. static LINENUM p_start;            /* where intuit found a patch */
  57. static LINENUM p_sline;            /* and the line number for it */
  58. static LINENUM p_hunk_beg;        /* line number of current hunk */
  59. static LINENUM p_efake = -1;        /* end of faked up lines--don't free */
  60. static LINENUM p_bfake = -1;        /* beg of faked up lines */
  61.  
  62. /* Prepare to look for the next patch in the patch file. */
  63.  
  64. void
  65. re_patch()
  66. {
  67.     p_first = Nulline;
  68.     p_newfirst = Nulline;
  69.     p_ptrn_lines = Nulline;
  70.     p_repl_lines = Nulline;
  71.     p_end = (LINENUM)-1;
  72.     p_max = Nulline;
  73.     p_indent = 0;
  74. }
  75.  
  76. /* Open the patch file at the beginning of time. */
  77.  
  78. void
  79. open_patch_file(filename)
  80. char *filename;
  81. {
  82.     if (filename == Nullch || !*filename || strEQ(filename, "-")) {
  83.     pfp = fopen(TMPPATNAME, "w");
  84.     if (pfp == Nullfp)
  85.         fatal2("patch: can't create %s.\n", TMPPATNAME);
  86.     while (fgets(buf, sizeof buf, stdin) != Nullch)
  87.         fputs(buf, pfp);
  88.     Fclose(pfp);
  89.     filename = TMPPATNAME;
  90.     }
  91.     pfp = fopen(filename, "r");
  92.     if (pfp == Nullfp)
  93.     fatal2("patch file %s not found\n", filename);
  94.     Fstat(fileno(pfp), &filestat);
  95.     p_filesize = filestat.st_size;
  96.     next_intuit_at(0L,1L);            /* start at the beginning */
  97.     set_hunkmax();
  98. }
  99.  
  100. /* Make sure our dynamically realloced tables are malloced to begin with. */
  101.  
  102. void
  103. set_hunkmax()
  104. {
  105. #ifndef lint
  106.     if (p_line == Null(char**))
  107.     p_line = (char**) malloc((MEM)hunkmax * sizeof(char *));
  108.     if (p_len == Null(short*))
  109.     p_len  = (short*) malloc((MEM)hunkmax * sizeof(short));
  110. #endif
  111.     if (p_char == Nullch)
  112.     p_char = (char*)  malloc((MEM)hunkmax * sizeof(char));
  113. }
  114.  
  115. /* Enlarge the arrays containing the current hunk of patch. */
  116.  
  117. void
  118. grow_hunkmax()
  119. {
  120.     hunkmax *= 2;
  121.     /* 
  122.      * Note that on most systems, only the p_line array ever gets fresh memory
  123.      * since p_len can move into p_line's old space, and p_char can move into
  124.      * p_len's old space.  Not on PDP-11's however.  But it doesn't matter.
  125.      */
  126.     assert(p_line != Null(char**) && p_len != Null(short*) && p_char != Nullch);
  127. #ifndef lint
  128.     p_line = (char**) realloc((char*)p_line, (MEM)hunkmax * sizeof(char *));
  129.     p_len  = (short*) realloc((char*)p_len,  (MEM)hunkmax * sizeof(short));
  130.     p_char = (char*)  realloc((char*)p_char, (MEM)hunkmax * sizeof(char));
  131. #endif
  132.     if (p_line != Null(char**) && p_len != Null(short*) && p_char != Nullch)
  133.     return;
  134.     if (!using_plan_a)
  135.     fatal1("patch: out of memory (grow_hunkmax)\n");
  136.     out_of_mem = TRUE;        /* whatever is null will be allocated again */
  137.                 /* from within plan_a(), of all places */
  138. }
  139.  
  140. /* True if the remainder of the patch file contains a diff of some sort. */
  141.  
  142. bool
  143. there_is_another_patch()
  144. {
  145.     if (p_base != 0L && p_base >= p_filesize) {
  146.     if (verbose)
  147.         say1("done\n");
  148.     return FALSE;
  149.     }
  150.     if (verbose)
  151.     say1("Hmm...");
  152.     diff_type = intuit_diff_type();
  153.     if (!diff_type) {
  154.     if (p_base != 0L) {
  155.         if (verbose)
  156.         say1("  Ignoring the trailing garbage.\ndone\n");
  157.     }
  158.     else
  159.         say1("  I can't seem to find a patch in there anywhere.\n");
  160.     return FALSE;
  161.     }
  162.     if (verbose)
  163.     say3("  %sooks like %s to me...\n",
  164.         (p_base == 0L ? "L" : "The next patch l"),
  165.         diff_type == CONTEXT_DIFF ? "a context diff" :
  166.         diff_type == NEW_CONTEXT_DIFF ? "a new-style context diff" :
  167.         diff_type == NORMAL_DIFF ? "a normal diff" :
  168.         "an ed script" );
  169.     if (p_indent && verbose)
  170.     say3("(Patch is indented %d space%s.)\n", p_indent, p_indent==1?"":"s");
  171.     skip_to(p_start,p_sline);
  172.     while (filearg[0] == Nullch) {
  173.     if (force) {
  174.         say1("No file to patch.  Skipping...\n");
  175.         filearg[0] = savestr(bestguess);
  176.         return TRUE;
  177.     }
  178.     ask1("File to patch: ");
  179.     if (*buf != '\n') {
  180.         if (bestguess)
  181.         free(bestguess);
  182.         bestguess = savestr(buf);
  183.         filearg[0] = fetchname(buf, 0, FALSE);
  184.     }
  185.     if (filearg[0] == Nullch) {
  186.         ask1("No file found--skip this patch? [n] ");
  187.         if (*buf != 'y') {
  188.         continue;
  189.         }
  190.         if (verbose)
  191.         say1("Skipping patch...\n");
  192.         filearg[0] = fetchname(bestguess, 0, TRUE);
  193.         skip_rest_of_patch = TRUE;
  194.         return TRUE;
  195.     }
  196.     }
  197.     return TRUE;
  198. }
  199.  
  200. /* Determine what kind of diff is in the remaining part of the patch file. */
  201.  
  202. int
  203. intuit_diff_type()
  204. {
  205.     Reg4 long this_line = 0;
  206.     Reg5 long previous_line;
  207.     Reg6 long first_command_line = -1;
  208.     long fcl_line;
  209.     Reg7 bool last_line_was_command = FALSE;
  210.     Reg8 bool this_is_a_command = FALSE;
  211.     Reg9 bool stars_last_line = FALSE;
  212.     Reg10 bool stars_this_line = FALSE;
  213.     Reg3 int indent;
  214.     Reg1 char *s;
  215.     Reg2 char *t;
  216.     char *indtmp = Nullch;
  217.     char *oldtmp = Nullch;
  218.     char *newtmp = Nullch;
  219.     char *indname = Nullch;
  220.     char *oldname = Nullch;
  221.     char *newname = Nullch;
  222.     Reg11 int retval;
  223.     bool no_filearg = (filearg[0] == Nullch);
  224.  
  225.     ok_to_create_file = FALSE;
  226.     Fseek(pfp, p_base, 0);
  227.     p_input_line = p_bline - 1;
  228.     for (;;) {
  229.     previous_line = this_line;
  230.     last_line_was_command = this_is_a_command;
  231.     stars_last_line = stars_this_line;
  232.     this_line = ftell(pfp);
  233.     indent = 0;
  234.     p_input_line++;
  235.     if (fgets(buf, sizeof buf, pfp) == Nullch) {
  236.         if (first_command_line >= 0L) {
  237.                     /* nothing but deletes!? */
  238.         p_start = first_command_line;
  239.         p_sline = fcl_line;
  240.         retval = ED_DIFF;
  241.         goto scan_exit;
  242.         }
  243.         else {
  244.         p_start = this_line;
  245.         p_sline = p_input_line;
  246.         retval = 0;
  247.         goto scan_exit;
  248.         }
  249.     }
  250.     for (s = buf; *s == ' ' || *s == '\t' || *s == 'X'; s++) {
  251.         if (*s == '\t')
  252.         indent += 8 - (indent % 8);
  253.         else
  254.         indent++;
  255.     }
  256.     for (t=s; isdigit(*t) || *t == ','; t++) ; 
  257.     this_is_a_command = (isdigit(*s) &&
  258.       (*t == 'd' || *t == 'c' || *t == 'a') );
  259.     if (first_command_line < 0L && this_is_a_command) { 
  260.         first_command_line = this_line;
  261.         fcl_line = p_input_line;
  262.         p_indent = indent;        /* assume this for now */
  263.     }
  264.     if (!stars_last_line && strnEQ(s, "*** ", 4))
  265.         o